#ifndef __CAN_IAP_H
#define __CAN_IAP_H

#include "stm32f10x.h"

#define RECV_DATA_ID 				(0x666)
#define RECV_FILEINFO_ID		(0x667)
#define SEND_REPLY					(0x668)

/* timeout */
#define TIMEOUT							(0x0600000)


/* ADDR */
#define FLASH_IAP_ADDR			(0x08000000)
#define FLASH_APP1_ADDR			(0x08003C00)
//#define FLASH_APP2_ADDR			(0x08010400)
#define FLASH_USER_ADDR			(0x0801CC00)


#define FLASH_PAGE_SIZE 		(0x400)
//#define FLASH_APP_SIZE			(0xC800)
#define FLASH_APP_SIZE			(0x19000)
#define UPGRADE_MARK_ADDR 	(FLASH_USER_ADDR)
#define FLASH_END						(0x08020000)

#define UPGRADE_WRITE_DATA	(0xCCCCCCCC)

/* reply */
enum
{
	REPLY_HANDSHAKE = 0x55,
	REPLY_ACK				= 0x41,
	REPLY_NAK				= 0x4E,
	REPLY_EOT				= 0x45,
	REPLY_BREAK_UP	= 0x42,
};

typedef union 
{
	uint8_t 	byte[8];
	uint32_t  word[2];
}WriteFlash_t;

typedef struct
{
	uint8_t flag;
	uint8_t len;
	uint8_t buf[8];
}CAN_MsgRecv_t;


/* funtion */
void IAP_CAN_Init(void);
uint8_t Send_Reply(uint8_t command);
uint8_t GetData(uint8_t *buf,uint8_t len);
int IAP_DownLoad(void);
uint8_t wait_HANDSHAKE(void);


#endif
